// import { login, logout, getInfo } from '@/api/user'
import { login } from '@/api/user'
import { getToken, setToken, setUser, getUser, removeToken, removeUser } from '@/utils/auth'
import { Message } from 'element-ui'
// import user from 'mock/user'
// import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

const state = {
  token: getToken() || null,
  // userInfo: get JSON.parse(getUser()) || null
  userInfo: getUser() ? JSON.parse(getUser()) : {}
}
const mutations = {
  updateToken(state, newToken) {
    state.token = newToken
    setToken(newToken)
  },
  updateUserInfo(state, newInfo) {
    state.userInfo = newInfo
    setUser(newInfo)
    // state.userInfo.avatar = '@/assets/common/avatar.jpg'
  },
  delToken(state) {
    removeToken()
    state.token = ''
  },
  delUserInfo() {
    removeUser()
    state.userInfo = {}
  }
}
const actions = {
  async login(context, payload) {
    const res = await login(payload)
    console.log(res)
    res.data.msg === 'ok' ? Message.success('登录成功') : Message.error(res.data.msg)
    context.commit('updateToken', res.data.data.token.token)
    context.commit('updateUserInfo', res.data.data.user)
  },
  logout(context) {
    context.commit('delToken')
    context.commit('delUserInfo')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

